<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>修改应用信息</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/js/layui/css/layui.css" media="all">
</head>
<body>
<div style="width:80%;height:630px;margin:15px auto;overflow:auto;" id="aaa">
    <form class="layui-form" action="" id="update_card">
        <input type="hidden" class="input-text" id="id" name="id">
        <div class="layui-form-item">
            <label class="layui-form-label">应用名称</label>
            <div class="layui-input-block">
                <input type="text" id="apkName" name="apkName" autocomplete="off" readonly class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">文件名称</label>
            <div class="layui-input-block">
                <input type="text" id="fileName" name="fileName" autocomplete="off" readonly class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">应用包名</label>
            <div class="layui-input-block">
                <input type="text" name="apkPackage" autocomplete="off" readonly class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">应用版本</label>
            <div class="layui-input-block">
                <input type="text" id="versions" name="versions" readonly autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">是否公开</label>
            <div class="layui-input-block">
                <select lay-verify="required" lay-search id="publicity">
                    <option value="0">未公开</option>
                    <option value="1">公开</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">是否启用</label>
            <div class="layui-input-block">
                <input type="radio" name="status" id="qiyong" value="1" title="启用" checked=""><div class="layui-unselect layui-form-radio layui-form-radioed"><i class="layui-anim layui-icon"></i>启用</div>
                <input type="radio" name="status" id="jinyong" value="0" title="禁用"><div class="layui-unselect layui-form-radio"><i class="layui-anim layui-icon"></i>禁用</div>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">创建时间</label>
            <div class="layui-input-block">
                <input type="text" name="createTime" autocomplete="off" readonly class="layui-input">
            </div>
        </div>

        <div class="layui-upload">
            <button type="button" class="layui-btn" id="test" style="display: inline;float: left;">应用图片</button>
            <div class="layui-upload-list" style="display: inline;margin-left: 30px;">
                <img class="layui-upload-img" name="picture" id="picture" style="width: 200px;height: 150px">
                <p id="demoText"></p>
            </div>
            <input type="hidden" class="input-text" id="apkPicture" name="apkPicture">
        </div>


        <div class="layui-form-item" style="margin-top: 50px">
            <div class="layui-input-block">
                <button class="layui-btn" id="updApk" lay-submit lay-filter="updApk">立即修改</button>
            </div>
        </div>
    </form>
</div>

<script src="/js/layui/layui.js"></script>
<script src="/js/layui/layui.all.js"></script>
<script src="/admin/js/token.js"></script>

<script>
    //url 传参
    function GetQueryString(name){
        var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
        var a = decodeURI(window.location.search);
        var r = a.substr(1).match(reg);//search,查询？后面的参数，并匹配正则
        if(r!=null)return  unescape(r[2]); return null;
    }

    window.onload = function(){
        var form = layui.form;
        var publicity = GetQueryString("publicity");
        $("input[name='apkName']").val(GetQueryString("apkName"));
        $("input[name='fileName']").val(GetQueryString("fileName"));
        $("input[name='apkPackage']").val(GetQueryString("apkPackage"));
        $("input[name='versions']").val(GetQueryString("versions"));
        $("input[name='apkPicture']").val(GetQueryString("picture"));
        $("input[name='createTime']").val(GetQueryString("createTime"));
        $("input[name='id']").val(GetQueryString("id"));
        $('#picture').attr('src', "/upload/"+GetQueryString("picture"));
        $('#publicity').find("option[value="+publicity+"]").attr("selected",true);
        form.render('select');
        var x = GetQueryString("status");
        if(x==1){
            $("#qiyong").prop("checked",true);
            form.render()
        }else{
            $("#jinyong").prop("checked",true);
            form.render()
        }
    }

    layui.use(['form' , 'jquery' , 'upload'], function(){
        var form = layui.form;
        var $ = layui.jquery;
        var upload = layui.upload;
        form.on('submit(updApk)',function(data){
            var publicity = $("#publicity").find("option:selected").val();
            var apkName = $("#apkName").find("option:selected").val();
            //var auditStatus = $("#auditStatus").find("option:selected").val();
            var id = $("#id").val();
            var fileName = $("#fileName").val();
            var apkPicture = $("input[name='apkPicture']").val();
            data.field.picture=apkPicture;
            data.field.file_name = fileName;
            data.field.publicity = publicity;
            //data.field.auditStatus = auditStatus;
            data.field.id=id;
            data.field.apk_name=apkName;
            $.ajax({
                url: "/apk/updApk"
                ,type:"POST"
                ,contentType: 'application/json'
                ,data:JSON.stringify(data.field)
                , success: function (data) {
                    if(data.code==2) {
                        layer.msg('修改应用失败！');
                    }else if(data.code==3){
                        layer.close(layer.index);
                        layer.msg('修改成功');
                        setTimeout(function(){
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);//关闭弹出层
                            parent.location.reload();//重新加载父页面表格
                        }, 2000);
                    }
                }
                , error: function () {
                    console.log("ajax error");
                }
            });
            return false;
        });

        //图片上传
        var uploadInst = upload.render({
            elem: '#test'
            , url: '/upload' //改成您自己的上传接口
            , before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#picture').attr('src', result); //图片链接（base64）
                });
            }
            , done: function (res) {
                //判断上传是否成功
                if (res.code == 0) {
                    $("#apkPicture").val(res.data);
                    return layer.msg('上传成功！');
                }else{
                    return layer.msg('上传失败！');
                }
            }
            , error: function () {
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #ff5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst.upload();
                });
            }
        });
    });
</script>
</body>
</html>